cc.Class({
    extends: cc.Component,

    properties: {
       moveDuration: 0.3,
       maxMoveSpeed: 100,
       accel:100
        
    },
    
    init(FXPlayer) {
        this.FXPlayer = FXPlayer;
    },
    hideFX:function() {
      this.FXPlayer.despawn();  
    },
    
    setInputControl: function () {
        var self = this;
        //add keyboard input listener to jump, turnLeft and turnRight
        cc.eventManager.addListener({
            event: cc.EventListener.KEYBOARD,
            // set a flag when key pressed
            onKeyPressed: function(keyCode, event) {
                switch(keyCode) {
                    case cc.KEY.a:
                    case cc.KEY.left:
                        self.accLeft = true;
                        self.accRight = false;
                        break;
                    case cc.KEY.d:
                    case cc.KEY.right:
                        self.accLeft = false;
                        self.accRight = true;
                        break;
                }
            },
            // unset a flag when key released
            onKeyReleased: function(keyCode, event) {
                switch(keyCode) {
                    case cc.KEY.a:
                    case cc.KEY.left:
                        self.accLeft = false;
                        break;
                    case cc.KEY.d:
                    case cc.KEY.right:
                        self.accRight = false;
                        break;
                }
            }
        }, self.node);

        // touch input
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            onTouchBegan: function(touch, event) {
                if(self.xSpeed === 0) {
                    self.xSpeed = 50;
                }
                
                var touchLoc = touch.getLocation();
                if (touchLoc.x >= cc.winSize.width/2) {
                    self.accLeft = false;
                    self.accRight = true;
                } else {
                    self.accLeft = true;
                    self.accRight = false;
                }
                // don't capture the event
                return true;
            },
            onTouchEnded: function(touch, event) {
                self.accLeft = false;
                self.accRight = false;
            }
        }, self.node);
    },
    
    
    setWalkAction: function(direction) {
        
        var walk2right = cc.moveBy(this.moveDuration, cc.p(0, 0)).easing(cc.easeCubicActionOut());
        
        var callback = cc.callFunc(this.walkAction(direction), this);
        
        return cc.repeatForever( cc.sequence( walk2right, callback));
    },
    
    // use this for initialization
    onLoad: function () {
        this.walk = this.getComponent('FXPlayer').init();
        
        this.accLeft = false;
        this.accRight = false;
        this.xSpeed = 0;
        
       // this.node.runAction(this.setWalkAction());
        
        this.setInputControl();
    },
    walkAction: function(direction) {
      this.walk.play(direction);  
    },

    // called every frame, uncomment this function to activate update callback
     update: function (dt) {
         // 根据当前加速度方向每帧更新速度
         
        if (this.accLeft) {
            //this.xSpeed -= this.accel * dt;
            this.xSpeed = 0 - Math.abs(this.xSpeed);
             this.walkAction(1);
        } else if (this.accRight) {
            //this.xSpeed += this.accel * dt;
            this.xSpeed = Math.abs(this.xSpeed);
            this.walkAction(2);
        }
        // 限制主角的速度不能超过最大值
        if ( Math.abs(this.xSpeed) > this.maxMoveSpeed ) {
            // if speed reach limit, use max speed with current direction
            this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
        }
        // 根据当前速度更新主角的位置
        this.node.x += this.xSpeed * dt;

        // limit player position inside screen
        if ( this.node.x > this.node.parent.width/2) {
            this.node.x = this.node.parent.width/2;
            this.xSpeed = 0;
        } else if (this.node.x < -this.node.parent.width/2) {
            this.node.x = -this.node.parent.width/2;
            this.xSpeed = 0;
        }
     },
});
